<?php

App::uses('AdminController', 'Controller');

class AdminPagesController extends AdminController {
        
    public function edit($page) {
        $this->set(compact('page'));
    }
    
    public function save($page) {
        $this->autoRender = false;
        $path = implode(DS, array(dirname(WWW_ROOT), 'View', 'Elements', 'pages', "{$page}.ctp"));
        $response = array('status'=>200, 'message' => 'Sačuvano');
        if (file_exists($path)) {
            if ($this->request->is('post')) {
                $html = $this->data['Page']['html'];
                if (! file_put_contents($path, $html)) {
                    $this->setFlash('Greška pri čuvanju podataka!', 'error');
                } else {
                    $this->setFlash('Sačuvano', 'ok');
                }
            } else {
                $this->setFlash('Neispravan zahtev!', 'error');
            }
        } else {
            $this->setFlash("Stranica '{$page}' nije nađena!", 'error');
        }
        
        
        $this->redirect(array(
            "action" => "edit",
            $page
        ));
    }
}
